﻿using System;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.BusinessLogic.Utils;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class MiniShoppingCartBoxControl : BaseNopFrontendUserControl
    {
        protected override void OnInit(EventArgs e) {
            PreRender += new EventHandler(MiniShoppingCartBoxControl_PreRender);
            rptCart.ItemDataBound += new RepeaterItemEventHandler(rptCart_ItemDataBound);

            base.OnInit(e);
        }

        void rptCart_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                    var sci = e.Item.DataItem as ShoppingCartItem;
                    if (sci != null) {
                        var hlProduct = e.Item.FindControl("hlProduct") as HyperLink;
                        if (hlProduct != null) {
                            hlProduct.NavigateUrl = SEOHelper.GetProductUrl(sci.ProductVariant.Product);
                            hlProduct.Text = Server.HtmlEncode(sci.ProductVariant.LocalizedFullProductName);
                        }

                        var lblQty = e.Item.FindControl("lblQty") as Label;
                        if (lblQty != null) {
                            lblQty.Text = string.Format("{0} x ", sci.Quantity);
                        }
                    }
            }
        }

        void MiniShoppingCartBoxControl_PreRender(object sender, EventArgs e) {
            if (this.SettingManager.GetSettingValueBoolean("Common.ShowMiniShoppingCart")) {
                var shoppingCart = this.ShoppingCartService.GetCurrentShoppingCart(ShoppingCartTypeEnum.ShoppingCart);
                if (shoppingCart.TotalProducts == 0) {
                    phCheckoutInfo.Visible = false;
                    lShoppingCart.Text = GetLocaleResourceString("MiniShoppingCartBox.NoItems");

                    rptCart.Visible = false;
                } else {
                    phCheckoutInfo.Visible = true;
                    if (shoppingCart.TotalProducts == 1) {
                        lShoppingCart.Text = string.Format(GetLocaleResourceString("MiniShoppingCartBox.OneItemText"), string.Format("<a href=\"{0}\" class=\"items\">{1}</a>", SEOHelper.GetShoppingCartUrl(), GetLocaleResourceString("MiniShoppingCartBox.OneItem")));
                    } else {
                        lShoppingCart.Text = string.Format(GetLocaleResourceString("MiniShoppingCartBox.SeveralItemsText"), string.Format("<a href=\"{0}\" class=\"items\">{1}</a>", SEOHelper.GetShoppingCartUrl(), string.Format(GetLocaleResourceString("MiniShoppingCartBox.SeveralItems"), shoppingCart.TotalProducts)));
                    }

                    lblOrderSubtotal.Text = GetLocaleResourceString("MiniShoppingCartBox.OrderSubtotal", GetOrderSubtotal(shoppingCart));

                    if (this.SettingManager.GetSettingValueBoolean("Display.ItemsInMiniShoppingCart", false)) {
                        rptCart.Visible = true;
                        rptCart.DataSource = shoppingCart;
                        rptCart.DataBind();
                    } else {
                        rptCart.Visible = false;
                    }

                }
            } else {
                this.Visible = false;
            }
        }

        protected string GetOrderSubtotal(ShoppingCart shoppingCart) {
            decimal subtotalBase = decimal.Zero;
            decimal orderSubTotalDiscountAmount = decimal.Zero;
            Discount orderSubTotalAppliedDiscount = null;
            decimal subTotalWithoutDiscountBase = decimal.Zero;
            decimal subTotalWithDiscountBase = decimal.Zero;
            string SubTotalError = this.ShoppingCartService.GetShoppingCartSubTotal(shoppingCart,
                NopContext.Current.User, out orderSubTotalDiscountAmount, out orderSubTotalAppliedDiscount,
                out subTotalWithoutDiscountBase, out subTotalWithDiscountBase);
            subtotalBase = subTotalWithoutDiscountBase;
            if (String.IsNullOrEmpty(SubTotalError)) {
                decimal subTotal = this.CurrencyService.ConvertCurrency(subtotalBase, this.CurrencyService.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                return PriceHelper.FormatPrice(subTotal);
            } else {
                return GetLocaleResourceString("MiniShoppingCartBox.OrderSubtotal.CalculatedDuringCheckout");
            }
        }
    }
}